//
//  RCMessageDetailDialogVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-11-14.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMessageDetailDialogVC.h"
#import "IManager+MessageDetail.h"
#import "RCMessageDetailDataModel.h"
#import "RCMessageDetailItemCell.h"
#import "AppDelegate.h"
#import "IManager+MessageDetail.h"
#import "NSString+NSString_Ex_.h"


@interface RCMessageDetailDialogVC ()<IManagerDelegate,UITableViewDataSource,UITableViewDelegate>{
    NSString *_userName;
    UITableView *_table;
    UIView      *_bottomView;
    UITextField *_textFiled;
    int64_t     _reqID;
    RCMessageDetailDataModel *_dataModel;
}
@property (nonatomic, retain) RCMessageDetailDataModel *dataModel;
@end

@implementation RCMessageDetailDialogVC
@synthesize dataModel = _dataModel;

- (id) initWithUserName:(NSString *)uName
{
    self = [super init];
    if (self) {
        _userName = [uName retain];
    }
    return self;
}

- (void) dealloc
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    self.dataModel = nil;
    [_userName release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if (_userName != nil) {
        self.titleLabelText = _userName;
        self.navigationItem.titleView = [self createTitleView];
    }
    [self refreshData];
    
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, UIScreenHeight-20-44-50)];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.viewContainer addSubview:_table];
    _table.delegate = self;
    _table.dataSource = self;
    [_table release];
    
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight-20-44-50, 320, 50)];
    [_bottomView setBackgroundColor:[UIColor grayColor]];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 230, 35)];
    [imageView setImage:[UIImage imageNamed:@"sendMsgBG"]];
    [_bottomView addSubview:imageView];
    [imageView release];
    
    UIButton *sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(250, 12, 60, 30)];
    [sendBtn setBackgroundImage:[UIImage imageNamed:@"sendMsg_Btn"] forState:UIControlStateNormal];
    [sendBtn addTarget:self action:@selector(sendMsgToServer) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:sendBtn];
    [sendBtn release];
    
    _textFiled = [[UITextField alloc] initWithFrame:CGRectMake(15, 10, 225, 35)];
    [_bottomView addSubview:_textFiled];
    _textFiled.placeholder = @"私信内容";
    [_textFiled release];
    
    [self.viewContainer addSubview:_bottomView];
    [_bottomView release];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardSizeWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

    
}




- (void) refreshData
{
    if (_userName != nil) {
        _reqID = [[IManager sharedInstance] requestMessageDetailWithUser:_userName withDelegate:self];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if ([dataModel isKindOfClass:[RCMessageDetailDataModel class]]) {
        self.dataModel = (RCMessageDetailDataModel *)dataModel;
        [_table reloadData];
        if (self.dataModel.err.errorType != RCErrorTypeNoError) {
            [RCAppDelegate alertWithCustomizeString:self.dataModel.errorMsg];
        }

        _textFiled.text = nil;


//        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:[self.dataModel.listItems count] inSection:0];
//        [_table scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        
    }else{
     
        [self refreshData];
    }
   
}

- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataModel.listItems count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCMessageDetailItemCell *cell = [[RCMessageDetailItemCell alloc] init];
    RCMessageDetailItemDataModel *item = [self.dataModel.listItems objectAtIndex:[indexPath row]];
    [cell bindData:item];

    return [cell autorelease];
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCMessageDetailItemDataModel *item = [self.dataModel.listItems objectAtIndex:[indexPath row]];

    return [RCMessageDetailItemCell cellHeight:item];
}


- (void) keyboardSizeWillChange:(NSNotification *)notification
{
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrameEndRect = [keyboardFrameEnd CGRectValue];
    
    CGRect tableFrame = _table.frame;
    CGRect bottomFrame = _bottomView.frame;
    tableFrame.size.height =UIScreenHeight-20-44-50 -(UIScreenHeight-keyboardFrameEndRect.origin.y);
    bottomFrame.origin.y = (UIScreenHeight-20-44-50-(UIScreenHeight - keyboardFrameEndRect.origin.y));
    
    _table.frame = tableFrame;
    _bottomView.frame = bottomFrame;
    
}


- (void) sendMsgToServer
{
    if (![NSString stringIsNullOrEmpty:_textFiled.text]) {
        [RCAppDelegate alertWithCustomizeString:@"提交"];
        [_textFiled resignFirstResponder];
        [[IManager sharedInstance] requestToUname:_userName withContent:_textFiled.text withDelegate:self];
    }
}
@end
